package org.javaqq.face.main.tree.menu;

import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;

import org.apache.log4j.Logger;
import org.javaqq.face.main.orgmeta.MetaFactry;
import org.javaqq.face.main.orgmeta.inter.MetaContactInter;
import org.javaqq.face.resources.Info;
import org.javaqq.face.resources.ResourcesManager;
import org.javaqq.face.util.ImageUtils;

public class ContactRightMenu extends JPopupMenu implements ActionListener {
	private final Logger logger = Logger.getLogger(ContactRightMenu.class);

	JTree tree;
	MetaContactInter contact;

	public ContactRightMenu(Object obj, JTree tree) {
		this.tree = tree;
		if (obj instanceof MetaContactInter) {
			contact = (MetaContactInter) obj;
			addContactMenuItem();
		}
	}

	public void addContactMenuItem() {
		add((JMenuItem) createTrayMenuItem("kongjianedit",Info.JAVAQQ_LANGUAGE_MENU_KONGJIANEDIT,this,true));
		add((JMenuItem) createTrayMenuItem("kongjian",Info.JAVAQQ_LANGUAGE_MENU_KONGJIAN,this,true));
		add((JMenuItem) createTrayMenuItem("ziliao",Info.JAVAQQ_LANGUAGE_MENU_ZILIAO,this,true));
		add((JMenuItem) createTrayMenuItem("updatehead",Info.JAVAQQ_LANGUAGE_MENU_UPDATEHEAD,this,true));
		add((JMenuItem) createTrayMenuItem("showhead",Info.JAVAQQ_LANGUAGE_MENU_SHOWHEAD,this,true));
		add((JMenuItem) createTrayMenuItem("beiwanglu",Info.JAVAQQ_LANGUAGE_MENU_BEIWANGLU,this,true));
		add((JMenuItem) createTrayMenuItem("liwu",Info.JAVAQQ_LANGUAGE_MENU_LIWU,this,true));
		add((JMenuItem) createTrayMenuItem("removeContact",Info.JAVAQQ_LANGUAGE_MENU_REMOVECONTACT,this,true));
	}

	private static Object createTrayMenuItem(String name, String textID,
			String iconID, ActionListener listener, boolean swing) {
		String text = ResourcesManager.getResourcesManagerFactory().getLanguageValue(textID);
		Object trayMenuItem;
		if (swing) {
			JMenuItem menuItem = new JMenuItem(text,
					new ImageIcon(ImageUtils.getImage(ResourcesManager.getResourcesManagerFactory()
							.getImageValue(iconID)))/*
													 * Resources.getImage(iconID)
													 */);
			menuItem.setName(name);
			menuItem.addActionListener(listener);
			trayMenuItem = menuItem;
		} else {
			MenuItem menuItem = new MenuItem(text);
			menuItem.setName(name);
			menuItem.addActionListener(listener);
			trayMenuItem = menuItem;
		}
		return trayMenuItem;
	}

	private static Object createTrayMenuItem(String name, String textID,
			ActionListener listener, boolean swing) {
		String text = ResourcesManager.getResourcesManagerFactory().getLanguageValue(textID);
		Object trayMenuItem;
		if (swing) {
			JMenuItem menuItem = new JMenuItem(text);
			menuItem.setName(name);
			menuItem.addActionListener(listener);
			trayMenuItem = menuItem;
		} else {
			MenuItem menuItem = new MenuItem(text);
			menuItem.setName(name);
			menuItem.addActionListener(listener);
			trayMenuItem = menuItem;
		}
		return trayMenuItem;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JMenuItem item=(JMenuItem) e.getSource();
		String itemName=item.getName();
		if(itemName.equals("removeContact")){
			removeContactFromGroup(contact);
		}
	}
	
	/**
     * Removes a contact item from the group.
     *
     * @param item the ContactItem to remove.
     */
    private void removeContactFromGroup(MetaContactInter metaContact) {
    	MetaFactry.removeContact(metaContact.getUserID());
    	MetaFactry.treeModel.reload();
    }

}
